package com.htd.erp.common;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.model.DataModel;
import javax.inject.Inject;
import javax.persistence.EntityManager;

import com.htd.erp.model.AbstractEntity;
import com.htd.erp.web.DataTable;

public abstract class EntityQuery<T extends AbstractEntity> implements Serializable {

	private static final long serialVersionUID = 8876169245546553517L;
	
	protected DataTable dataTable;
	
	protected List<String> dataColumns;
	
	@DataRepository
	@Inject
	private EntityManager entityManager;
	
	@PostConstruct
	public void postConstruct() {
		dataColumns = buildDataColumns();
		dataTable = buildDataTable();
	}
	
	abstract public DataModel<T> getDataModel();
	
	abstract protected List<String> buildDataColumns();
	
	protected DataTable buildDataTable() {
		DataTable table = new DataTable();
		for(String property : getDataColumns()) {
			table.registProperty(property);
		}
		return table;
	}
	
	public List<String> getDataColumns() {
		return dataColumns;
	}
	
	protected List<String> buildDataColumns(String... properties) {
		List<String> columns = new ArrayList<String>(properties.length);
		for(String property : properties) {
			columns.add(property);
		}
		return columns;
	}
	
	public DataTable getDataTable() {
		return dataTable;
	}

	protected EntityManager getEntityManager() {
		return entityManager;
	}
}
